//
//  PP_SelWithDrawViewController.m
//  PPDL
//
//  Created by 王洋 on 2019/11/4.
//  Copyright © 2019 王洋. All rights reserved.
//

#import <WXApi.h>

#import "AFNetworking.h"

#import "WithdrawViewController.h"

#import "PP_SelWithDrawViewController.h"


#import "PP_AlipayAccountUpdateViewController.h"


@interface PP_SelWithDrawViewController ()<UITextFieldDelegate>

//支付宝
@property (weak, nonatomic) IBOutlet UIView *alipayView;

//微信
@property (weak, nonatomic) IBOutlet UIView *weChatView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *weChatHeight;

//银行卡
@property (weak, nonatomic) IBOutlet UIView *bankView;

//钱的输入框
@property (weak, nonatomic) IBOutlet UITextField *moneyTF;


@property (nonatomic, assign) BOOL isWait;

@end

@implementation PP_SelWithDrawViewController

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"PP_SendAuthRespNotification" object:nil];

}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(SendAuthRespCallBack:) name:@"PP_SendAuthRespNotification" object:nil];
}

-(void)textFieldDidChange:(UITextField *)textField{
    
    //过滤空格
    NSString *tem = [[textField.text componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] componentsJoinedByString:@""];
    textField.text = tem;
}

// 只能输入数字和小数点
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:@".0123456789"] invertedSet];
    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
    return [string isEqualToString:filtered];
}

- (void)initView {
    
    self.isWait = NO;
    self.title = @"选择提现方式";
    
    self.moneyTF.placeholder = [NSString stringWithFormat:@"可提现余额:%@", [Utilities changeDoubleToString:self.model.itemWithDrawMoney]];
    self.moneyTF.delegate = self;
    
    self.alipayView.userInteractionEnabled = YES;
    self.weChatView.userInteractionEnabled = YES;
    self.bankView.userInteractionEnabled = YES;
    
    if (![WXApi isWXAppInstalled]) {
        self.weChatHeight.constant = CGFLOAT_MIN;
    }
    
    WEAKSELF
    [self.alipayView addGestureTapEventHandle:^(id  _Nonnull sender, UITapGestureRecognizer * _Nonnull gestureRecognizer) {

        if (!weakSelf.isWait) {
            if (!weakSelf.moneyTF.text.length) {
                weakSelf.isWait = NO;
                [weakSelf alert:@"请输入提现金额"];
                return;
            }
            [weakSelf updateAlipayAccount];
        } else {
            
        }

    }];
    
    [self.weChatView addGestureTapEventHandle:^(id  _Nonnull sender, UITapGestureRecognizer * _Nonnull gestureRecognizer) {
        
        if (!weakSelf.isWait) {
            
            weakSelf.isWait = YES;
            if (!weakSelf.moneyTF.text.length) {
                weakSelf.isWait = NO;
                [weakSelf alert:@"请输入提现金额"];
                return;
            }
            [weakSelf request_WithDrawApply];
            
        } else {
            
        }
    }];
    
    [self.bankView addGestureTapEventHandle:^(id  _Nonnull sender, UITapGestureRecognizer * _Nonnull gestureRecognizer) {
        
        if (!weakSelf.isWait) {

            if (!weakSelf.moneyTF.text.length) {
                weakSelf.isWait = NO;
                [weakSelf alert:@"请输入提现金额"];
                return;
            }
            [weakSelf bankWithDraw];
            
        } else {
            
        }
    }];
}

-(void)SendAuthRespCallBack:(NSNotification *)Noti {
    
    NSString *code = [Noti.userInfo valueForKey:@"code"];
    [self request_WXOpenIDWithCode:code];
    
    NSLog(@"收到一条h微信回调");
}

- (void)request_WXOpenIDWithCode:(NSString *)code {
    
    NSDictionary *dic = @{}.mutableCopy;
    [dic setValue:code forKey:@"code"];
    
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,TT_LOGIN_WXAUTH];
    
    WEAKSELF
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        if ([data isKindOfClass:[NSDictionary class]]) {
            
            NSString * code = [data valueForKey:@"code"];
            if ([code isEqualToString:@"200"]) {
                
                //code给接口成功后     请求提现
                [weakSelf request_WithDrawApply];
            } else {
                
                [self alert:[data valueForKey:@"msg"]];
                
            }
        } else {
            
        }

    } FailedBlock:^(NSError * _Nonnull error) {
        [self alert:@"请求失败"];

    }];
}

- (void)request_WithDrawApply {
    
    NSDictionary *dic = @{}.mutableCopy;
    [dic setValue:self.moneyTF.text forKey:@"amount"];
    [dic setValue:@"2" forKey:@"clientType"];// 客户端类型 1.微信小程序 2.APP 5.公众号
    [dic setValue:@"1" forKey:@"type"];// 转账方式 0银行卡打款 1微信转账 2支付宝转账
    
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,TT_MEMBER_WITHDRAW_APPLY_V2];
    WEAKSELF
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        NSLog(@"请求一次");
        
        self.isWait = NO;
        
        if ([data isKindOfClass:[NSDictionary class]]) {
            
            NSString * code = [data valueForKey:@"code"];
            
            NSInteger status = [[data valueForKey:@"status"] integerValue];
            
            if (status == 3) {
                
                //微信授权
                [weakSelf sendAuthRequest];
                
            } else {
                
                if ([code isEqualToString:@"200"]) {
                    
                    [self.navigationController popToRootViewControllerAnimated:YES];
                    [self alert:[data valueForKey:@"msg"]];
                    
                } else {
                    
                    [self alert:[data valueForKey:@"msg"]];
                }
            }

        } else {
            
        }
        
        
    } FailedBlock:^(NSError * _Nonnull error) {
        
        self.isWait = NO;
        [self alert:@"请求失败"];
    }];    
}

-(void)sendAuthRequest
{
    //构造SendAuthReq结构体
    SendAuthReq* req =[[SendAuthReq alloc]init];
    req.scope = @"snsapi_userinfo";
    req.state = @"123";
    //第三方向微信终端发送一个SendAuthReq消息结构
    [WXApi sendReq:req];
}

//添加支付宝提现账号
- (void)updateAlipayAccount {
    PP_AlipayAccountUpdateViewController *VC = [[PP_AlipayAccountUpdateViewController alloc] init];
    VC.amount = self.moneyTF.text;
    [self.navigationController pushViewController:VC animated:YES];
}

- (void)bankWithDraw {
    
    WithdrawViewController *VC = [[WithdrawViewController alloc] init];
    VC.amount = self.moneyTF.text;
    [self.navigationController pushViewController:VC animated:YES];
}

- (IBAction)withDrawAllMoney:(id)sender {
    
    self.moneyTF.text = [NSString stringWithFormat:@"%@",[Utilities changeDoubleToString:self.model.itemWithDrawMoney]];
    
}


@end
